﻿using System;

public class Mapper182 : MapperDefault
{
    private int irq_counter;
    private bool irq_enabled;
    private int[] regs = new int[1];

    public override void init(NES nes)
    {
        base.init(nes);
        this.reset();
    }

    public override void loadROM(ROM rom)
    {
        if (!rom.Valid)
        {
            Console.Out.WriteLine("182: Invalid ROM! Unable to load.");
        }
        else
        {
            int num = rom.RomBankCount * 2;
            this.load8kRomBank(0, 0x8000);
            this.load8kRomBank(1, 0xa000);
            this.load8kRomBank(num - 2, 0xc000);
            this.load8kRomBank(num - 1, 0xe000);
            this.loadCHRROM();
            base.nes.Cpu.requestIrq(2);
        }
    }

    public override void reset()
    {
        this.irq_enabled = false;
        this.irq_counter = 0;
    }

    public override int syncH(int scanline)
    {
        if (((this.irq_enabled && (scanline >= 0)) && ((scanline <= 240) && ((base.ppu.scanline & 0x18) != 0))) && (--this.irq_counter == 0))
        {
            this.irq_counter = 0;
            this.irq_enabled = false;
            return 3;
        }
        return 0;
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            switch ((address & 0xf003))
            {
                case 0x8001:
                    if ((value_Renamed & 1) != 0)
                    {
                        base.nes.Ppu.Mirroring = 1;
                        return;
                    }
                    base.nes.Ppu.Mirroring = 0;
                    return;

                case 0xa000:
                    this.regs[0] = value_Renamed & 7;
                    break;

                case 0xc000:
                    switch (this.regs[0])
                    {
                        case 0:
                            this.load2kVromBank(value_Renamed, 0);
                            return;

                        case 1:
                            this.load1kVromBank(value_Renamed, 0x1400);
                            return;

                        case 2:
                            this.load2kVromBank(value_Renamed, 0x800);
                            return;

                        case 3:
                            this.load1kVromBank(value_Renamed, 0x1c00);
                            return;

                        case 4:
                            this.load8kRomBank(value_Renamed, 0x8000);
                            return;

                        case 5:
                            this.load8kRomBank(value_Renamed, 0xa000);
                            return;

                        case 6:
                            this.load1kVromBank(value_Renamed, 0x1000);
                            return;

                        case 7:
                            this.load1kVromBank(value_Renamed, 0x1800);
                            return;
                    }
                    return;

                case 0xe003:
                    this.irq_counter = value_Renamed;
                    this.irq_enabled = value_Renamed != 0;
                    return;
            }
        }
    }
}

